package com.yyy.dm;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtils {
    private static String SECRET = "token!Q@W#E$R";


    public static String getToken(String key,Map map) {
        JWTCreator.Builder builder = JWT.create();
//        map.forEach((k, v) -> {
//            builder.withClaim(key, map);
//        });
        builder.withClaim(key,map);
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.HOUR, 2); //默认7天过期
        builder.withExpiresAt(instance.getTime());//指定令牌的过期时间
        String token = builder.sign(Algorithm.HMAC256(SECRET));//签名
        return token;
    }

    public static DecodedJWT verify(String token) {
        //如果有任何验证异常，此处都会抛出异常
        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
        return decodedJWT;
    }

}